/**
 * @file
 * @author jinguangguo
 * @date 2017/9/11
 */

import lib from '../../common/lib';
import commonService from '../../common/commonService';

$(function () {

    // 充值
    (function () {
        let $recharge = $('.module-rechage');
        let $elem = $('.js-recharge');
        let $menu = $recharge.find('.menu-recharge');

        if (lib.isMobile() === false) {
            $elem.on('mouseenter', (e) => {
                $menu.show();
                $menu.off('mouseenter').on('mouseenter', (e) => {
                    $menu.show();
                });
                $menu.off('mouseleave').on('mouseleave', (e) => {
                    $menu.hide();
                });
            });

            $elem.on('mouseleave', (e) => {
                $menu.hide();
            });
        } else {
            $elem.on('click', (e) => {
                if (this.show === true) {
                    $menu.hide();
                } else {
                    $menu.show();
                }
            });
        }

        $('.js-rechargeUsc').click(function (e) {
            if (lib.isLogin() === true) {
                window.open('/cn/recharge_usc.html');
                // window.location.href = '/cn/recharge_usc.html';
                // console.log('登陆了');
            } else {
                let fromPath = window.encodeURIComponent(window.location.href.replace('index.html', 'recharge_usc.html'));
                window.open('./login.html?from=' + fromPath);
                // window.location.href = './login.html?from=' + fromPath;

                // window.error('请登陆后，再领取，3秒后为您跳转登陆页面');
                // setTimeout(()=>{
                //     let fromPath = window.encodeURIComponent(window.location.href.replace('index.html','get-presend.html'));
                //     window.location.href = './login.html?from=' + fromPath;
                // },3000);
                // console.log('没登陆');
            }
        });
    })();


    let lang = window.location.pathname.split('/')[1]; // us / es / hk
    $(document).on('click', '.js-lang', (e) => {
        // $('.js-lang').click(function(e) {
        let $this = $(e.target).closest('.js-lang');
        // 获取当前页面
        let targetLang = $this.data('lang');
        let href = window.location.href;
        let pathname = window.location.pathname;

        let targetHref;
        if (lang !== 'us' && lang !== 'es' && lang !== 'hk' && lang !== 'cn') {
            targetHref = window.location.origin + '/' + targetLang + pathname;
        } else {
            targetHref = href.replace(`/${lang}/`, `/${targetLang}/`);
        }

        // console.log(targetHref);
        window.location.href = targetHref;
        // });
    });
    //暂未开放提示
    $(document).on('click', '.js-c2c', () => {
            window.error('暂未开放')      
    })

    // 退出
    $(document).on('click', '.js-logout', () => {
        let header = {
            'account-no': lib.getAccountNo()
        };
        commonService.logout({}, header).done(rep => {
            if (rep.code === window.CODE.SUCCESS) {
                let userData = localStorage.getItem('userLogin');
                if (userData != 'null' && userData != null && userData != undefined && userData != 'undefined') {
                    userData = JSON.parse(userData);
                    userData.password = '';
                    userData.isRemember = false;
                    localStorage.setItem('userLogin', JSON.stringify(userData) + '');
                }
                lib.doLogout();
            } else {
                window.error(rep.msg, () => {
                    window.location.href = "./";
                });
            }
        }).fail(rep => {
            window.error(rep.msg, () => {
                window.location.href = "./market.html?s=";
            });
        });
    })

    //判断登陆态
    if (lib.isLogin() === false) {
        $('.js-do-not-login').show();
        // $('.js-item').show();
    } else {
        $('.js-do-login').show();
        // $('.js-item').show();
        $('.js-account').show();
    }




    //只有首页需要加顶部点亮效果
    let pathname = window.location.pathname;
    if (pathname.indexOf('index.html') > 0 || pathname === '/' || pathname === '/us/' || pathname === '/es/' || pathname === '/hk/' || pathname === '/cn/') {
        let $nav = $('.layout-nav');
        let $window = $(window);
        $window.scroll(function () {
            if ($window.scrollTop() === 0) {
                $nav.removeClass('layout-nav--index--bg');
            } else {
                $nav.addClass('layout-nav--index--bg');
            }
        })
    }



    //悬浮（点击）显示 右上角标
    let $coming = $('.item-link--coming');
    let $tip = $('.sub-tip-top');
    $coming.hover(function () {
        $tip.stop().fadeIn(300);
    }, function () {
        $tip.stop().fadeOut(300);
    });


    // 埋点统计相关  如果测试环境不做统计
    if (lib.isNeedStat()) {

        // 首页 交易按钮
        $('.layout-nav--index .stat-market').click(() => {
            lib.stat(['_trackEvent', '首页', '交易点击'], () => {
                window.location.href = './market.html?s=';
            })
        });

        // 首页 交易按钮
        $('.layout-nav--index .stat-notice').click(() => {
            lib.stat(['_trackEvent', '首页', '公告点击'], () => {
                window.location.href = '/cn/notice.html';
            })
        });

        // 首页 登陆
        $('.layout-nav--index .stat-login').click(() => {
            lib.stat(['_trackEvent', '首页', '登陆点击'], () => {
                window.location.href = './login.html';
            })
        });

        // 首页 注册
        $('.layout-nav--index .stat-register').click(() => {
            lib.stat(['_trackEvent', '首页', '注册点击'], () => {
                window.location.href = './register.html';
            })
        });

        // 首页 购买比特币
        $('.layout-nav--index .stat-buybitcoin').click(() => {
            lib.stat(['_trackEvent', '首页', '购买比特币点击']);
        });
    }























});